package smartclass.adapter;

import java.util.ArrayList;

import com.jeremyfeinstein.slidingmenu.example.R;

import smartclass.entity.ItemGridPage;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ItemGridAdapter extends ArrayAdapter<ItemGridPage> {

	Context context;
	int resource;
	ArrayList<ItemGridPage> array;

	public ItemGridAdapter(Context context, int resource,
			ArrayList<ItemGridPage> array) {
		super(context, resource, array);
		// TODO Auto-generated constructor stub
		this.context = context;
		this.resource = resource;
		this.array = array;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub

		View row = convertView;
		ItemHolder holder = null;

		if (row == null) {
			LayoutInflater inflater = LayoutInflater.from(context);
			row = inflater.inflate(resource, parent, false);
			holder = new ItemHolder(row);
			row.setTag(holder);
		} else {
			holder = (ItemHolder) row.getTag();
		}

		holder.populateFrom(array.get(position));

		return row;

	}

	class ItemHolder {

		private ImageView imgPage;
		private TextView tvPageIndex;

		ItemHolder(View row) {
			tvPageIndex = (TextView) row.findViewById(R.id.tvPageIndex);
			imgPage = (ImageView) row.findViewById(R.id.imgPage);

		}

		void populateFrom(final ItemGridPage r) {
			tvPageIndex.setText(r.getNumber());
			tvPageIndex.setTextColor(Color.RED);
			imgPage.setImageBitmap(r.getImage());
		}

	}

}
